package ventana;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import mapa.Mapa;

import terrenos.Bosque;
import terrenos.Casilla;
import terrenos.Hierba;
import terrenos.Montanya;
import terrenos.Tierra;
import excepciones.CreateMapaException;

public class CreadorMapas extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textFieldX = new JTextField(4);
	private JTextField textFieldY = new JTextField(4);
	private JTextField textFieldId = new JTextField(4);
	private JTextField tfBosque = new JTextField(80);
	private JTextField tfHierba = new JTextField(80);
	private JTextField tfMontaya = new JTextField(80);
	private JTextField tfTierra = new JTextField(80);
	private JTextField tfPath = new JTextField(80);
	private JButton crear = new JButton("Crear");
	private JLabel estado = new JLabel(
			"Las coordenadas deben estar en este formato-> (x1,y1);(x2,y2);(x3,y3)... no acaba en ;");
	private Casilla[][] tablero = null;

	public CreadorMapas() {
		setSize(new Dimension(1000, 400));
		setTitle("Creador de mapas");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		JPanel superior = new JPanel();
		JPanel centro = new JPanel();
		centro.setLayout(new GridLayout(6, 1));

		superior.add(new JLabel("X:"));
		superior.add(textFieldX);
		superior.add(new JLabel("Y: "));
		superior.add(textFieldY);
		superior.add(new JLabel("ID:"));
		superior.add(textFieldId);
		add(superior, "North");
		posicionarDatos(centro, "Bosque", tfBosque);
		posicionarDatos(centro, "Hierba", tfHierba);
		posicionarDatos(centro, "Montaya", tfMontaya);
		posicionarDatos(centro, "Tierra", tfTierra);
		posicionarDatos(centro, "Path (guardar .map)", tfPath);
		JPanel temp = new JPanel();
		temp.add(crear);
		centro.add(temp);
		add(centro, "Center");
		add(estado, "South");

		crear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				iniciar();
			}
		});
	}

	private void iniciar() {
		tablero = new Casilla[Integer.parseInt(textFieldX.getText())][Integer
				.parseInt(textFieldY.getText())];
		if (tfBosque.getText().length() != 0) {
			insertarBosque();
		}
		if (tfHierba.getText().length() != 0) {
			insertarHierba();
		}
		if (tfMontaya.getText().length() != 0) {
			insertarMontaya();
		}
		if (tfTierra.getText().length() != 0) {
			insertarTierra();
		}

		try {
			guardarMapa();
		} catch (Exception e) {
			estado.setText(e.getMessage());
		}
	}

	private void insertarHierba() {
		String[] array1 = tokenizar1(tfHierba.getText());
		for (String s : array1) {
			Integer[] array2 = tokenizar2(s);
			int x = array2[0];
			int y = array2[1];
			tablero[x][y] = new Hierba();
		}
	}

	private void insertarBosque() {
		String[] array1 = tokenizar1(tfBosque.getText());
		for (String s : array1) {
			Integer[] array2 = tokenizar2(s);
			int x = array2[0];
			int y = array2[1];
			tablero[x][y] = new Bosque();
		}
	}

	private void insertarMontaya() {
		String[] array1 = tokenizar1(tfMontaya.getText());
		for (String s : array1) {
			Integer[] array2 = tokenizar2(s);
			int x = array2[0];
			int y = array2[1];
			tablero[x][y] = new Montanya();
		}
	}

	private void insertarTierra() {
		String[] array1 = tokenizar1(tfTierra.getText());
		for (String s : array1) {
			Integer[] array2 = tokenizar2(s);
			int x = array2[0];
			int y = array2[1];
			tablero[x][y] = new Tierra();
		}
	}

	private String[] tokenizar1(String s) {
		String[] array = s.split(";");
		return array;
	}

	public static Integer[] tokenizar2(String s) {
		String[] array = s.split(",");
		Integer[] arrayInt = new Integer[2];
		for (String cadena : array) {
			if ((cadena.charAt(0)) == '(') {
				arrayInt[0] = Integer.parseInt(cadena.substring(1));
			} else {
				arrayInt[1] = Integer.parseInt(cadena.substring(0,
						cadena.length() - 1));
			}

		}
		return arrayInt;
	}

	private void guardarMapa() throws Exception {
		FileOutputStream fMapa = new FileOutputStream(tfPath.getText());
		ObjectOutputStream fichero = new ObjectOutputStream(fMapa);
		Mapa mapa = new Mapa(Integer.parseInt(textFieldX.getText()),
				Integer.parseInt(textFieldY.getText()));
		mapa.setTerreno(tablero);
		mapa.setIdMapa(Integer.parseInt(textFieldId.getText()));
		fichero.writeObject(mapa);

		fichero.writeObject(null);
	
		fichero.close();
		estado.setText("Guardado");
	}

	private void posicionarDatos(JPanel c, String etiqueta, Component componente) {
		JPanel temporal = new JPanel();
		temporal.setLayout(new FlowLayout(FlowLayout.LEFT));
		temporal.add(new JLabel(etiqueta));
		temporal.add(componente);
		c.add(temporal);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				CreadorMapas c = new CreadorMapas();
				c.setVisible(true);

			}
		});
		

	}
}
